package com.sctvcloud.bazhong.ui.adapter.holder;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.adapter.holder.BaseRecylerHolder;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.ui.widget.TextViewExpandableAnimation;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.beans.FComment;
import com.sctvcloud.bazhong.ui.utils.GlideCircleTransform;
import com.sctvcloud.bazhong.ui.utils.GlideUtil;
import com.sctvcloud.bazhong.utils.UserManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liuyu on 2017/8/22.
 */

public class CLCommentHolder extends BaseRecylerHolder<FComment> implements View.OnClickListener {

    @BindView(R.id.comment_layout)
    protected LinearLayout commentLayouot;
    @BindView(R.id.comment_more)
    protected LinearLayout commentMoreLayouot;


    @BindView(R.id.normal_layout)
    protected RelativeLayout normalLayouot;

    @BindView(R.id.item_comment_icon)
    protected ImageView icon;
    @BindView(R.id.item_comment_name)
    protected CustomFontTextView name;
    @BindView(R.id.item_comment_reply)
    protected CustomFontTextView replay;
    @BindView(R.id.item_comment_time)
    protected CustomFontTextView time;
    @BindView(R.id.item_comment_content)
    protected TextViewExpandableAnimation content;
    @BindView(R.id.item_comment_divider)
    protected View divider;

    @BindView(R.id.answer_layout)
    protected RelativeLayout answerLayouot;
    @BindView(R.id.answer_item_comment_icon)
    protected ImageView answer_icon;
    @BindView(R.id.answer_item_comment_name)
    protected CustomFontTextView answer_name;
    @BindView(R.id.answer_item_comment_reply)
    protected CustomFontTextView answer_replay;
    @BindView(R.id.answer_item_comment_time)
    protected CustomFontTextView answer_time;
    @BindView(R.id.answer_item_comment_content)
    protected TextViewExpandableAnimation answer_content;
    @BindView(R.id.answer_item_comment_divider)
    protected View answerDivider;
    @BindView(R.id.item_comment_bottom_divider)
    protected View itemBottom;

    private FComment fComment;
    private boolean lastCommentIsReply = false;
    private List<FComment> list;

    public CLCommentHolder(final Context context, List<FComment> list, final View itemView) {
        super(context, itemView);
        this.list = list;
        replay.setOnClickListener(this);
        content.setOnStateChangeSetting(new TextViewExpandableAnimation.OnStateChangeSetting() {
            @Override
            public void reInitStateView(TextView stateView) {
//                stateView.setBackgroundResource(R.drawable.bg_comment_all_l);
                stateView.setText(R.string.newes_see_all);
//                ViewGroup.MarginLayoutParams mlp= (ViewGroup.MarginLayoutParams) stateView.getLayoutParams();
//                mlp.topMargin= DensityUtil.dip2px(context,18);
//                stateView.setLayoutParams(mlp);
            }

            @Override
            public void onStateChanged(TextView stateView, boolean isShrink) {
                JLog.e(" is shrink=" + isShrink + "   " + this);
                content.getStateImg().setRotation(isShrink ? 90 : -90);
            }
        });
        commentMoreLayouot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (internalClick != null) {
                    internalClick.onItemInternalClick(itemView, v, getAdapterPosition());
                }
            }
        });
        content.setOnStateChangeListener(new TextViewExpandableAnimation.OnStateChangeListener() {
            @Override
            public void onStateChange(boolean isShrink) {
                if (fComment != null) {
                    fComment.setShrink(isShrink);
                }
            }
        });
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(FComment fComment) {
        itemBottom.setVisibility(View.GONE);
        if (fComment.getViewType() == -1) {
            commentLayouot.setVisibility(View.GONE);
            commentMoreLayouot.setVisibility(View.VISIBLE);
        } else {
            commentLayouot.setVisibility(View.VISIBLE);
            commentMoreLayouot.setVisibility(View.GONE);

            int position = getAdapterPosition();
            if (null != list && list.size() > position && position > 0) {
                if (!TextUtils.isEmpty(list.get(position - 1).getAnswerTo())) {
                    //上一条为回复评论
                    lastCommentIsReply = true;
                } else {
                    lastCommentIsReply = false;
                }
            }
            if (TextUtils.isEmpty(fComment.getAnswerTo())) {
                if (position == 0) {
                    divider.setVisibility(View.GONE);
                } else {
                    divider.setVisibility(View.VISIBLE);
                }
                normalLayouot.setVisibility(View.VISIBLE);
                answerLayouot.setVisibility(View.GONE);
                name.setText(fComment.getNickName());
                replay.setTag(R.id.adapter_item_data_key, fComment);
                if (UserManager.getInstance().getUser() != null) {
                    replay.setVisibility(View.VISIBLE);
                }
                time.setText(DateUtils.timeAgo(DateUtils.getDataDate(fComment)));
                content.setText(fComment.getComment());
                content.resetState(fComment.isShrink());
                if (!TextUtils.isEmpty(fComment.getAvatar())) {
                    GlideUtil.getAvatarGlid(context, fComment.getAvatar(), true).into(icon);
//                    GlideUtil.getGlideWithAvatorDef(context, fComment.getAvatar()).skipMemoryCache(true).error(R.mipmap.icon_circle_avatar)
//                            .placeholder(R.mipmap.icon_circle_avatar).transform(new GlideCircleTransform(context)).into(icon);
                }
            } else {
                normalLayouot.setVisibility(View.GONE);
                answerLayouot.setVisibility(View.VISIBLE);

                answer_name.setText(fComment.getNameSpanned());
                answer_replay.setTag(R.id.adapter_item_data_key, fComment);
                answer_replay.setVisibility(View.GONE);
                answer_time.setText(DateUtils.timeAgo(DateUtils.getDataDate(fComment)));
                answer_content.setText(fComment.getComment());
                if (lastCommentIsReply) {
                    answerDivider.setVisibility(View.VISIBLE);
                } else {
                    answerDivider.setVisibility(View.GONE);
                }
                answer_content.resetState(fComment.isShrink());
                if (!TextUtils.isEmpty(fComment.getAvatar())) {
                    GlideUtil.getAvatarGlid(context, fComment.getAvatar(), true).into(answer_icon);
//                    GlideUtil.getGlideWithAvatorDef(context, fComment.getAvatar()).skipMemoryCache(true).error(R.mipmap.icon_circle_avatar)
//                            .placeholder(R.mipmap.icon_circle_avatar).transform(new GlideCircleTransform(context)).into(answer_icon);
                }

            }
            if (position == list.size() - 1) {
                itemBottom.setVisibility(View.VISIBLE);
            } else {
                itemBottom.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        JLog.e("replay click internal is " + internalClick);
        if (internalClick != null) {
            internalClick.onItemInternalClick(itemView, v, getAdapterPosition());
        }
    }

}
